package com.moorecat.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

/**
 * 文件操作类
 *
 * @author Minglong.Yu
 * @version 1.0
 * @created Jun 11, 2011 5:08:24 PM
 */
public class FileUtils {
    private static final String TAG = "FileUtils";

    /**
     * 获取输入流的字符内容
     *
     * @param is
     * @return
     */
    public static String toText(InputStream is) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int length;
        try {
            while ((length = is.read(bytes)) != -1) {
                outputStream.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return outputStream.toString();
    }

    /**
     * 从android的assets目录中获取文件内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getAssertFileContent(Context context, String fileName) {
        String text = null;
        AssetManager am = context.getAssets();
        InputStream is = null;
        try {
            is = am.open(fileName);
            text = FileUtils.toText(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return text;
    }

    /**
     * 判断文件是否过期
     *
     * @param file
     * @param expire
     * @return
     */
    public static boolean isExpire(File file, long expire) {
        boolean rs = false;
        long now = new Date().getTime();
        if (file.isFile()) {
            long mtime = file.lastModified();
            Log.d(TAG, "now = " + now + "\t modified time= " + mtime + "\t diff time = " + (now - mtime) +
                    " \t result = " + ((now - mtime) > expire));
            if ((now - mtime) > expire) {
                rs = true;
            } else {
                rs = false;
            }
        }
        Log.v(TAG, "!!!! The return result: "+rs);
        return rs;
    }

    /**
     * 获取目录下的文件按照时间排序
     *
     * @param dir
     * @param isAsc 是否升序
     * @return
     */
    public static File[] getFilesOrderByModified(String dir, final boolean isAsc) {
        File[] files = null;
        File directory = new File(dir);
        if (directory.exists() && directory.isDirectory()) {
            files = directory.listFiles();
            Arrays.sort(files, new Comparator<File>() {
                public int compare(File file, File file1) {
                    int rs = 0;
                    long diffTime = file.lastModified() - file1.lastModified();
                    if (diffTime > 0) {
                        rs = 1;
                    } else if (diffTime == 0) {
                        rs = 0;
                    } else {
                        rs = -1;
                    }
                    if (!isAsc) {
                        rs = rs * -1;
                    }
                    return rs;
                }
            });
        }
        return files;
    }

    /**
     * 删除过期的文件
     *
     * @param dir
     * @param expire
     * @return
     */
    public static long deleteExpireFile(String dir, long expire) {
        long size = 0;
        File directory = new File(dir);
        if (directory.exists()) {
            File[] list = directory.listFiles();
            if (list != null && list.length > 0) {
                long now = new Date().getTime();
                for (File file : list) {
                    if (file.isFile()) {
                        long mtime = file.lastModified();
                        if ((now - mtime) > expire) {
                            Log.d(TAG, "!!!!! Delete File:" + file.getAbsolutePath());
                            if (file.delete())
                                size++;
                        }
                    }
                }
            }
        }
        return size;
    }

}
